<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;

class JialianController extends PayController{
    protected $config;
    protected $key;
    protected $url;

    public function pay($array){
        $this->config = M("Channel")->where(array('id'=>$array['channel']))->find();
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'018']);
        }

        $product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'018']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $pay_memberid = $this->config['mch_id'];   //商户ID
        $pay_orderid = $return['orderid'];
        $pay_amount = $return['amount'];
        $pay_applydate = date("Y-m-d H:i:s",time());  //订单时间
        $pay_bankcode = 949;   //银行编码
        $pay_notifyurl = $return['notifyurl'];
        $pay_callbackurl = $return['callbackurl'];

        $Md5key = $this->config['signkey'];  //密钥

        $jsapi = array(
            "pay_memberid" => $pay_memberid,
            "pay_orderid" => $pay_orderid,
            "pay_amount" => $pay_amount,
            "pay_applydate" => $pay_applydate,
            "pay_bankcode" => $pay_bankcode,
            "pay_notifyurl" => $pay_notifyurl,
            "pay_callbackurl" => $pay_callbackurl,
        );

        ksort($jsapi);

        $md5str = "";
        foreach ($jsapi as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }

        $sign = strtoupper(md5($md5str . "key=" . $Md5key));


        $jsapi["pay_md5sign"] = $sign;
        $jsapi["pay_productname"] = '会员服务'; //商品名称
        $jsapi["pay_attach"] = '原样返回字段';

        $res = curl($this->config['gateway'], httpstr($jsapi));
        $res = json_decode($res, true);
        if(empty($res)){
            $this->showmessage('获取数据失败!',['code'=>'018']);
        }

        $code = $res['url'];

        if(!$code){
            $this->showmessage("获取二维码失败", ['code'=>'018']);
        }

        header("Location: {$code}");
    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true."\r\n\r\n"), FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date('Y-m-d H:i:s',time())."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n");
        //file_put_contents("a.txt",date("Y-m-d H:i:s",time()) . "\r\n", FILE_APPEND);

        $p = $_POST;
        /*$p = array (
            'returncode' => '00',
            'memberid' => '10046',
            'orderid' => '201808181640527010182',
            'transaction_id' => '20180818164116995197',
            'amount' => '10.00',
            'datetime' => '2018-08-18 16:47:11',
            'sign' => '277A1F76EDD14A0EF3CF56CAD638BDD9',
            'attach' => '原样返回字段',
        );*/

        //获取订单信息
        $orderid = $p['orderid'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array("server_return"=> json_encode($p),"pay_orderid_rd"=>$p['transaction_id']));
        Order::getInstance()->edit($find['id'],array("server_return"=> json_encode($p), "pay_orderid_rd"=>$p['transaction_id']));
        Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array("server_return"=> json_encode($p), "pay_orderid_rd"=>$p['transaction_id']));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Jialian'))->find();

        $ReturnArray = array( // 返回字段
            "memberid" => $p["memberid"], // 商户ID
            "orderid" =>  $p["orderid"], // 订单号
            'transaction_id' => $p["transaction_id"],
            "amount" =>  $p["amount"], // 交易金额
            "datetime" =>  $p["datetime"], // 交易时间
            "returncode" => $p["returncode"]
        );

        $key = $this->config['signkey'];

        ksort($ReturnArray);

        $str = "";
        foreach ($ReturnArray as $k => $v) {
            $str .= $k."=".$v."&";
        }
        $str .= "key=".$key;

        $sign = $p['sign'];
        $sign2 = strtoupper(md5($str));

        if($sign != $sign2){
            exit("sign_wrong");
        }
        if($p['returncode'] != '00'){
            exit("pay_fail");
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Jialian",0);

        //回调成功
        echo 'ok'; exit;
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}